package com.mmall.controller.backstage;

import com.mmall.common.ServerResponse;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 商品分类管理
 * Created by qezhhnjy on 2017/9/26-16:28.
 */
@Controller
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Resource
    private IUserService iUserService;

    @Resource
    private ICategoryService iCategoryService;

    /**
     * 添加品类
     *
     * @param session      当前会话
     * @param categoryName 品类名称
     * @param parentId     品类父类id,默认为0
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "add.do", method = RequestMethod.POST)
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId", defaultValue = "0") Integer parentId) {
/*        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), "用户未登录,请重新登录!");
        //校验是否时管理员
        if (iUserService.adminCheck(user).isSuccess()) {
            //处理分类逻辑
            return iCategoryService.addCategory(categoryName, parentId);
        } else return ServerResponse.createByErrorMessage("该用户无操作权限!");*/
        ServerResponse response = iUserService.adminCheck(session);
        if (response.isSuccess())
            return iCategoryService.addCategory(categoryName, parentId);
        return response;
    }

    /**
     * 修改品类名称
     *
     * @param session      当前会话
     * @param categoryId   品类id
     * @param categoryName 品类名称
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "modify_name.do", method = RequestMethod.POST)
    public ServerResponse modifyCategoryName(HttpSession session, Integer categoryId, String categoryName) {
/*
        User user = (User) session.getAttribute(IConst.CURRENT_USER);
        if (user == null)
            return ServerResponse.createByErrorCode(ResponseInfo.NEED_LOGIN.getCode(), "用户未登录,请重新登录!");
        //校验是否时管理员
        if (iUserService.adminCheck(user).isSuccess()) {
            //处理分类逻辑
            return null;
        } else return ServerResponse.createByErrorMessage("该用户无操作权限!");
*/
        ServerResponse response = iUserService.adminCheck(session);
        if (response.isSuccess())
            return iCategoryService.updateName(categoryId, categoryName);
        return response;
    }

    /**
     * 查询指定Id 的下一级子节点,不递归,保持平级
     *
     * @param session    当前回话
     * @param categoryId 品类id
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "get_child.do", method = RequestMethod.POST)
    public ServerResponse getChildrenParallelCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        ServerResponse response = iUserService.adminCheck(session);
        if (response.isSuccess())
            return iCategoryService.getChildrenParallel(categoryId);
        return response;
    }

    /**
     * 查询指定id 的子节点,且递归查询到最底层
     *
     * @param session    当前回话
     * @param categoryId 品类id
     * @return 响应对象
     */
    @ResponseBody
    @RequestMapping(value = "get_all_child.do", method = RequestMethod.POST)
    public ServerResponse getAllChildren(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        ServerResponse response = iUserService.adminCheck(session);
        if (response.isSuccess())
            return iCategoryService.getAllChildren(categoryId);
        return response;
    }
}
